package com.yugao.fintech.libra.workflow.controller;

import com.yugao.fintech.meheco.core.web.domain.AjaxResult;
import com.yugao.fintech.libra.workflow.domain.dto.FlowTaskDto;
import com.yugao.fintech.libra.workflow.domain.vo.FlowTaskVo;
import com.yugao.fintech.libra.workflow.service.IFlowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


@RestController
@RequestMapping("/task")
@Tag(name = "工作流流程任务管理")
public class FlowTaskController {
    @Autowired
    private IFlowTaskService flowTaskService;

    @Operation(description = "撤销流程", response = FlowTaskDto.class)
    @PostMapping(value = "/stopProcess")
    public ResultVO stopProcess(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.stopProcess(flowTaskVo);
        return ResultVO.success();
    }

    @Operation(description = "获取待办列表", response = FlowTaskDto.class)
    @GetMapping(value = "/todoList")
    public ResultVO todoList(@ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum,
                               @ApiParam(value = "每页条数", required = true) @RequestParam Integer pageSize) {
        return ResultVO.success(flowTaskService.todoList(pageNum, pageSize));
    }

    @Operation(description = "获取已办任务", response = FlowTaskDto.class)
    @GetMapping(value = "/finishedList")
    public ResultVO finishedList(@ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum,
                                   @ApiParam(value = "每页条数", required = true) @RequestParam Integer pageSize) {
        return ResultVO.success(flowTaskService.finishedList(pageNum, pageSize));
    }

    @Operation(description = "流程历史流转记录", response = FlowTaskDto.class)
    @GetMapping(value = "/flowRecord")
    public ResultVO flowRecord(String procInsId, String deployId) {
        return ResultVO.success(flowTaskService.flowRecord(procInsId, deployId));
    }

    @Operation(description = "获取流程变量", response = FlowTaskDto.class)
    @GetMapping(value = "/processVariables/{taskId}")
    public ResultVO processVariables(@ApiParam(value = "流程任务Id") @PathVariable(value = "taskId") String taskId) {
        return ResultVO.success(flowTaskService.processVariables(taskId));
    }

    @Operation(description = "审批任务")
    @PostMapping(value = "/complete")
    public ResultVO complete(@RequestBody FlowTaskVo flowTaskVo) {
        return ResultVO.success(flowTaskService.complete(flowTaskVo));
    }

    @Operation(description = "删除任务")
    @DeleteMapping(value = "/delete")
    public ResultVO delete(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.deleteTask(flowTaskVo);
        return ResultVO.success();
    }

    @Operation(description = "获取下一节点")
    @PostMapping(value = "/nextFlowNode")
    public ResultVO getNextFlowNode(@RequestBody FlowTaskVo flowTaskVo) {
        return ResultVO.success(flowTaskService.getNextFlowNode(flowTaskVo));
    }

    /**
     * 生成流程图
     *
     * @param processId 任务ID
     */
    @RequestMapping("/diagram/{processId}")
    public void genProcessDiagram(HttpServletResponse response,
                                  @PathVariable("processId") String processId) {
        InputStream inputStream = flowTaskService.diagram(processId);
        OutputStream os = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(inputStream);
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 生成流程图
     *
     * @param procInsId 流程实例编号
     * @param procInsId 任务执行编号·
     */
    @RequestMapping("/flowViewer/{procInsId}/{executionId}")
    public ResultVO getFlowViewer(@PathVariable("procInsId") String procInsId,
                                    @PathVariable("executionId") String executionId) {
        return ResultVO.success(flowTaskService.getFlowViewer(procInsId, executionId));
    }
}
